<template>
  <view class="loginson">
    <u--form :labelWidth="60" :rules="rules" :model="form" ref="uForm">
      <u-form-item label="手机号" prop="phone">
        <u-input type="number" v-model="form.phone" />
      </u-form-item>

      <u-form-item label="密码" prop="password">
        <u-input v-model="form.password" />
      </u-form-item>
    </u--form>
    <u-button @click="submit">提交</u-button>
    <view class="box"
      >没有账号，前往<text class="register" @click="register()"
        >注册</text
      ></view
    >
  </view>
</template>

<script>
import { mapMutations, mapState } from "vuex";
import { login } from "../../request/API/login";
export default {
  data() {
    return {
      form: {
        phone: "",
        password: "",
      },
      rules: {
        phone: [
          {
            required: true,
            message: "请输入手机号",
            trigger: ["blur", "change"],
          },
          {
            // 自定义验证函数，见上说明
            validator: (rule, value, callback) => {
              // 上面有说，返回true表示校验通过，返回false表示不通过
              // uni.$u.test.mobile()就是返回true或者false的
              return uni.$u.test.mobile(value);
            },
            message: "请输入正确的手机号",
            // 触发器可以同时用blur和change
            trigger: ["change", "blur"],
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: ["blur", "change"],
          },
          // 正则判断为字母或数字
          {
            pattern: /^[0-9a-zA-Z]*$/g,
            // 正则检验前先将值转为字符串
            transform(value) {
              return String(value);
            },
            message: "只能包含字母或数字",
          },
          // 6-8个字符之间的判断
          {
            min: 6,
            max: 8,
            message: "请输入6-8位包含字母数字的密码",
          },
        ],
      },
    };
  },
  computed: {
    ...mapState(["token", "isLogin"]),
  },
  methods: {
    ...mapMutations(["setToken", "setIsLogin"]),
    submit() {
      this.$refs.uForm
        .validate()
        .then((res) => {
          uni.$u.toast("校验通过");
          // 接口
          login({
            phone: this.form.phone,
            password: this.form.password,
          })
            .then((res) => {
              console.log(res.data.token,'token');
              this.setToken(res.data.token);
              this.setIsLogin(true);
              const name = res.data.name
              this.$store.commit('setUserName',name)
              const phone = res.data.phone
              this.$store.commit('setUserPhone',phone)
              const sex = res.data.sex
              this.$store.commit('setUserSex',sex)
              const id = res.data.id
              this.$store.commit('setUserId',id)
              uni.setStorage({
                key: "token",
                data: res.data.token,
                success: function () {
                  console.log("success");
                },
              });
              uni.switchTab({
                url: "/pages/user/user",
              });
            })
          // 接口
        })
        .catch((errors) => {
          uni.$u.toast("校验失败");
        });
    },
    register() {
      uni.navigateTo({
        url: "/pages/login/register",
      });
    },
  },
};
</script>


<style lang="scss">
.loginson {
  width: 94%;
  margin: auto;
}
.box {
  margin-top: 40rpx;
  .register {
    margin-left: 20rpx;
    color: #3f9cd6;
    font-size: 1.2rem;
    font-weight: bold;
  }
}
</style>
